package com.gec.anan.rules.config;


import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DroolConfig {
    //工厂对象
    private static final KieServices kieServices = KieServices.Factory.get();

    //配置文件的路劲
    private static final String RULES_CUSTOMER_RULES_DRL = "rules/FeeRule.drl";

    @Bean
    public KieContainer kieContainer() {
        //文件流对象
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        //根据路径读取目标配置文件
        KieFileSystem fileSystem =  kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_CUSTOMER_RULES_DRL));

        KieBuilder kieBuilder = kieServices.newKieBuilder(fileSystem);
        //根据配置文件创建出来的模型
        KieModule kieModule = kieBuilder.getKieModule();

        //返回的是容器
        KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
        return kieContainer;
    }
}
